package com.ztgf.shopping.service.template;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.db.utils.RedisUtil;
import com.ztgf.search.models.dto.EsShopDTO;
import com.ztgf.search.models.service.IShopSearchService;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.shop.ShopTipMapper;
import com.ztgf.shopping.mapper.template.TempletInfoMapper;
import com.ztgf.shopping.models.bo.epg.*;
import com.ztgf.shopping.models.bo.template.ShopActiveRelationSaveBO;
import com.ztgf.shopping.models.bo.template.TempletInfoSaveBO;
import com.ztgf.shopping.models.bo.template.TempletModuleBO;
import com.ztgf.shopping.models.bo.template.TempletPlatformRelationSaveBO;
import com.ztgf.shopping.models.dto.epg.*;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.dto.template.ShopActiveRelationDTO;
import com.ztgf.shopping.models.dto.template.TempletInfoDTO;
import com.ztgf.shopping.models.dto.template.TempletModuleDTO;
import com.ztgf.shopping.models.dto.template.TempletPlatformRelationDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.template.ITempletInfoService;
import com.ztgf.shopping.models.vo.epg.*;
import com.ztgf.shopping.models.vo.template.TemplateHomePageOrderCountVO;
import com.ztgf.shopping.models.vo.template.TemplateInfoDVO;
import com.ztgf.shopping.models.vo.template.TempletInfoVO;
import com.ztgf.shopping.models.vo.template.TempletModuleVO;
import com.ztgf.shopping.service.epg.*;
import com.ztgf.shopping.service.shop.ShopInfoServiceImpl;
import com.ztgf.sys.models.dto.SysPlatformDTO;
import com.ztgf.sys.models.service.ISysPlatformService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 模版  服务实现类
 * </p>
 *
 * @author YUE
 * @since 2020-05-23
 */
@Slf4j
@DubboService
public class TempletInfoServiceImpl extends ServiceImpl<TempletInfoMapper, TempletInfoDTO> implements ITempletInfoService {
    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private TempletInfoMapper templetInfoMapper;

    @Autowired
    private TempletPlatformRelationServiceImpl templetPlatformRelationService;

    @Autowired
    private EpgShopBannerServiceImpl epgShopBannerService;

    @Autowired
    private EpgBannerServiceImpl epgBannerService;

    @Autowired
    private EpgTopicServiceImpl epgTopicService;

    @Autowired
    private EpgShowWindowServiceImpl epgShowWindowService;

    @Autowired
    private EpgShopMainNavigationServiceImpl epgShopMainNavigationService;

    @Autowired
    private EpgMainNavigationServiceImpl epgMainNavigationService;

    @Autowired
    private EpgShopSectionServiceImpl epgShopSectionService;

    @Autowired
    private EpgSectionServiceImpl epgSectionService;

    @Autowired
    private EpgShopSectionCommodityRelationServiceImpl epgShopSectionCommodityRelationService;

    @Autowired
    private EpgSectionCommodityRelationServiceImpl epgSectionCommodityRelationService;

    @Autowired
    private TempletModuleServiceImpl templetModuleService;

    @Autowired
    private EpgZtgfInfoServiceImpl epgZtgfInfoService;

    @Autowired
    @Lazy
    private ShopInfoServiceImpl shopInfoService;

    @Autowired
    private EpgAggregationBannerServiceImpl epgAggregationBannerService;

    @Autowired
    private EpgAggregationCategoryServiceImpl epgAggregationCategoryService;

    @Autowired
    private EpgAggregationCategoryCommodityRelationServiceImpl epgAggregationCategoryCommodityRelationService;

    @Autowired
    private ShopActiveRelationServiceImpl shopActiveRelationService;

    @DubboReference
    private ISysPlatformService sysPlatformService;

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ShopTipMapper shopTipMapper;

    @Value("${page.show.url}")
    private String pageShowUrl;

    @DubboReference
    private IShopSearchService shopSearchService;

    /**
     * 创建一个默认的平台活动页模板
     */
    private Long createDefaultPlateActiveTemplate(TempletInfoSaveBO templetInfoSaveBO, Long parentId) {
        Long templateId = addAndReturnId(templetInfoSaveBO);
        //创建模板关联关系表
        TempletPlatformRelationSaveBO templetPlatformRelationSaveBO = new TempletPlatformRelationSaveBO();
        templetPlatformRelationSaveBO.setTempletId(templateId);
        templetPlatformRelationSaveBO.setShopId(0L);
        templetPlatformRelationSaveBO.setType(ShoppingCodeEnum.TEMPLATE_TYPE_ACTIVE.getCode());
        templetPlatformRelationSaveBO.setParentTemplateId(parentId);
        templetPlatformRelationService.add(templetPlatformRelationSaveBO);
        return templateId;
    }

    /**
     * 根据店铺id创建默认的
     *
     * @param shopId
     */
    /*@Async*/
    @Override
    public void createDefaultTemplateByShopId(Long shopId) {
        //创建默认店铺首页模板
        TempletInfoSaveBO templetInfoSaveBO = new TempletInfoSaveBO();
        //templetInfoSaveBO.setType(2);
        templetInfoSaveBO.setSort(0);
        templetInfoSaveBO.setName("默认模板");
        Long shopHomeTemplateId = addAndReturnId(templetInfoSaveBO);
        //创建默认店铺活动页模板
        //templetInfoSaveBO.setType(1);
        Long shopActiveTemplateId = addAndReturnId(templetInfoSaveBO);
        //创建默认商品详情页的模板banner
        Long shopBannerTemplateId = addAndReturnId(templetInfoSaveBO);
        //关联店铺模板表（首页）
        TempletPlatformRelationSaveBO templetPlatformRelationSaveBO = new TempletPlatformRelationSaveBO();
        templetPlatformRelationSaveBO.setShopId(shopId);
        templetPlatformRelationSaveBO.setTempletId(shopHomeTemplateId);
        templetPlatformRelationSaveBO.setIsDefault(ShoppingCodeEnum.IS_DEFAULT_TRUE.getCode());
        templetPlatformRelationSaveBO.setType(ShoppingCodeEnum.TEMPLATE_TYPE_HOME.getCode());
        templetPlatformRelationService.add(templetPlatformRelationSaveBO);
        //关联店铺模板表（活动页）
        templetPlatformRelationSaveBO.setTempletId(shopActiveTemplateId);
        templetPlatformRelationSaveBO.setType(ShoppingCodeEnum.TEMPLATE_TYPE_ACTIVE.getCode());
        templetPlatformRelationService.add(templetPlatformRelationSaveBO);
        //关联店铺模板表（商品详情页banner）
        templetPlatformRelationSaveBO.setTempletId(shopBannerTemplateId);
        templetPlatformRelationSaveBO.setType(ShoppingCodeEnum.TEMPLATE_TYPE_COMMODITY_DETAIL.getCode());
        templetPlatformRelationService.add(templetPlatformRelationSaveBO);
        //关联店铺首页和活动页模板的关联关系
        ShopActiveRelationSaveBO shopActiveRelationSaveBO = new ShopActiveRelationSaveBO();
        shopActiveRelationSaveBO.setShopTempletId(shopHomeTemplateId);
        shopActiveRelationSaveBO.setActivityTempletId(shopActiveTemplateId);
        shopActiveRelationSaveBO.setShopId(shopId);
        shopActiveRelationSaveBO.setIsDefault(ShoppingCodeEnum.IS_DEFAULT_TRUE.getCode());
        shopActiveRelationService.add(shopActiveRelationSaveBO);
        //添加店铺首页模板的默认功能（banner,shopInfo,section(新品推荐，镇店之宝，掌柜推荐)）
        templetModuleService.createDefaultShopHomeModule(shopId, shopHomeTemplateId);
        //添加店铺活动页模板的默认功能（banner,,分类(3个)）
        templetModuleService.createDefaultShopActiveModule(shopActiveTemplateId);
        //添加店铺商品详情页banner模板的默认功能（banner)
        templetModuleService.createDefaultShopBannerModule(shopBannerTemplateId);
    }

    /**
     * 根据模板id获取下面的装修模板
     *
     * @param templateId
     * @return
     */
    @Override
    public Result<List<TemplateInfoDVO>> getTemplateDecorationByTemplateId(Long templateId) {
        List<TemplateInfoDVO> templateInfoDVOList = new ArrayList<>();
        List<TempletPlatformRelationDTO> templetPlatformRelationDTOList = templetPlatformRelationService.getByParentId(templateId);
        for (TempletPlatformRelationDTO templetPlatformRelationDTO : templetPlatformRelationDTOList) {
            TempletInfoDTO templetInfoDTO = selectById(templetPlatformRelationDTO.getTempletId());
            TemplateInfoDVO templateInfoDVO = new TemplateInfoDVO();
            BeanUtils.copyProperties(templetInfoDTO, templateInfoDVO);
            templateInfoDVO.setIsDefault(templetPlatformRelationDTO.getIsDefault());
            templateInfoDVOList.add(templateInfoDVO);
        }
        return new Result<>(templateInfoDVOList);
    }

    @Override
    public TemplateHomePageOrderCountVO getHomepageUserOrderStatusStatistics(Long shopId) {
        TemplateHomePageOrderCountVO templateHomePageOrderCountVO = new TemplateHomePageOrderCountVO();
        // 待确认
        Integer a = shopTipMapper.getBuyerOrderStateNum(shopId, 0,0);
        // 待付款
        Integer b = shopTipMapper.getTheBuyerSPendingPaymentQuantity(shopId,0);
        // 合同待签定
        Integer c = shopTipMapper.getBuyerOrderStateNum(shopId, 1,0);
        // 待发货
        Integer d = shopTipMapper.getBuyerOrderStateNum(shopId, 3,0);
        // 待签收
        Integer e = shopTipMapper.getBuyerOrderStateNum(shopId, 4,0);


        // 待确认
        Integer a1 = shopTipMapper.getBuyerOrderStateNum(shopId, 0,1);
        // 待付款
        Integer b1 = shopTipMapper.getTheBuyerSPendingPaymentQuantity(shopId,1);
        // 合同待签定
        Integer c1 = shopTipMapper.getBuyerOrderStateNum(shopId, 1,1);
        // 待发货
        Integer d1 = shopTipMapper.getBuyerOrderStateNum(shopId, 3,1);
        // 待签收
        Integer e1 = shopTipMapper.getBuyerOrderStateNum(shopId, 4,1);
        templateHomePageOrderCountVO.setToBeConfirmed(a+a1);
        templateHomePageOrderCountVO.setPendingPayment(b+b1);
        templateHomePageOrderCountVO.setToBeSigned(c+c1);
        templateHomePageOrderCountVO.setToBeDelivered(d+d1);
        templateHomePageOrderCountVO.setToBeReceived(e+e1);
        TemplateHomePageOrderCountVO homepageUserOrderStatusStatistics = baseMapper.getHomepageUserOrderStatusStatistics(shopId);
        templateHomePageOrderCountVO.setNewNews(homepageUserOrderStatusStatistics.getNewNews());
        return templateHomePageOrderCountVO;
    }

    @Override
    public Result deleteActivityPageById(Long id) {
        Result result = new Result<>();
        // 先查询模板是否为默认模板
        Wrapper<TempletPlatformRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("templet_id", id);
        TempletPlatformRelationDTO selectOne = templetPlatformRelationService.selectOne(wrapper);
        if (selectOne != null && selectOne.getIsDefault() == 0) {
            result.setCode(CodeEnum.FAIL_100050.getCode());
            result.setMsg(CodeEnum.FAIL_100050.getMsg());
            return result;
        }
        TempletInfoDTO select = this.selectById(id);
        if (select == null) {
            result.setCode(CodeEnum.FAIL_100051.getCode());
            result.setMsg(CodeEnum.FAIL_100051.getMsg());
            return result;
        }
        // 删除templet_info
        this.deleteById(id);
        // 删除templet_module
        Wrapper<TempletModuleDTO> wrapperA = new EntityWrapper<>();
        wrapperA.eq("templet_id", select.getId());
        templetModuleService.delete(wrapperA);
        // 删除templet_platform_relation
        templetPlatformRelationService.delete(wrapper);
        // 删除epg_aggregation_banner
        Wrapper<EpgAggregationBannerDTO> wrapperB = new EntityWrapper<>();
        wrapperB.eq("templet_id", select.getId());
        epgAggregationBannerService.delete(wrapperB);
        // 删除epg_aggregation_category
        Wrapper<EpgAggregationCategoryDTO> wrapperC = new EntityWrapper<>();
        wrapperC.eq("templet_id", select.getId());
        List<EpgAggregationCategoryDTO> list = epgAggregationCategoryService.selectList(wrapperC);
        if (list != null && list.size() > 0) {
            for (EpgAggregationCategoryDTO dto : list) {
                // 删除epg_aggregation_category_commodity_relation
                Wrapper<EpgAggregationCategoryCommodityRelationDTO> wrapperD = new EntityWrapper<>();
                wrapperD.eq("category_id", dto.getId());
                epgAggregationCategoryCommodityRelationService.delete(wrapperD);
            }
            epgAggregationCategoryService.delete(wrapperC);
        }
        result.setCode(CodeEnum.SUCCESS.getCode());
        result.setMsg(CodeEnum.SUCCESS.getMsg());
        return result;
    }

    @Override
    public Result savePlatformHome(List<TempletModuleBO> list) {
        for (TempletModuleBO templetModuleBO : list) {
            Long templetModuleId = templetModuleBO.getId();
            Long templetId = templetModuleBO.getTempletId();
            TempletModuleDTO dto = templetModuleService.selectById(templetModuleId);
            if (dto == null) {
                return new Result<>(CodeEnum.FAIL_100002);
            }
            Map<String, String> map = templetModuleBO.getMap();
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_BANNER.getMsg())) {//banner处理
                //先删除之前的banner
                epgBannerService.deleteByModuleId(templetModuleId);
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_BANNER.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    EpgBannerSaveBO epgBannerSaveBO = new EpgBannerSaveBO();
                    epgBannerSaveBO.setBannerType(epgDecorationBOList.get(i).getBannerType());
                    epgBannerSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                    epgBannerSaveBO.setModuleId(templetModuleId);
                    epgBannerSaveBO.setSkipUrlPc(epgDecorationBOList.get(i).getSkipUrl());
                    epgBannerSaveBO.setSkipType(epgDecorationBOList.get(i).getSkipType());
                    epgBannerSaveBO.setTempletId(templetId);
                    epgBannerSaveBO.setSort(i + 1);
                    epgBannerSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgBannerSaveBO.setLeftBgcolor(epgDecorationBOList.get(i).getLeftBgcolor());
                    epgBannerSaveBO.setRightBgcolor(epgDecorationBOList.get(i).getRightBgcolor());
                    epgBannerService.add(epgBannerSaveBO);
                }
            }
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_TOPIC.getMsg())) {//活动
                //先删除之前的推荐专区
                epgTopicService.deleteByModuleId(templetModuleId);
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_TOPIC.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    EpgTopicSaveBO epgTopicSaveBO = new EpgTopicSaveBO();
                    epgTopicSaveBO.setSkipUrlPc(epgDecorationBOList.get(i).getSkipUrl());
                    epgTopicSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                    epgTopicSaveBO.setModuleId(templetModuleId);
                    epgTopicSaveBO.setTempletId(templetId);
                    epgTopicSaveBO.setSort(i + 1);
                    epgTopicSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgTopicService.add(epgTopicSaveBO);
                }
            }
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_ZTGF_INFO.getMsg())) {
                //先删除之前的品牌推荐专区
                setModuleContent(templetId, templetModuleId, ShoppingCodeEnum.DECORATE_ZTGF_INFO.getMsg(), map);
            }
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_SECTION.getMsg())) {//频道
                String mapValue = map.get(ShoppingCodeEnum.DECORATE_SECTION.getMsg());
                log.info("频道装修mapValue:{}", mapValue);
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(mapValue, EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    Long id = epgDecorationBOList.get(i).getId();
                    if (id == null) {
                        EpgSectionSaveBO epgSectionSaveBO = new EpgSectionSaveBO();
                        epgSectionSaveBO.setModuleId(templetModuleId);
                        epgSectionSaveBO.setTempletId(templetId);
                        epgSectionSaveBO.setSort(i + 1);
                        epgSectionSaveBO.setName(epgDecorationBOList.get(i).getName());
                        epgSectionSaveBO.setNote(epgDecorationBOList.get(i).getContent());
                        epgSectionSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                        epgSectionSaveBO.setKey(epgDecorationBOList.get(i).getKey());
                        epgSectionSaveBO.setFirstCategory(epgDecorationBOList.get(i).getFirstCategory());
                        epgSectionSaveBO.setZeroCategory(epgDecorationBOList.get(i).getZeroCategory());
                        id = epgSectionService.addAndReturnId(epgSectionSaveBO);
                    } else {
                        EpgSectionDTO epgSectionDTO = epgSectionService.selectById(id);
                        epgSectionDTO.setName(epgDecorationBOList.get(i).getName());
                        epgSectionDTO.setZeroCategory(epgDecorationBOList.get(i).getZeroCategory());
                        epgSectionDTO.setFirstCategory(epgDecorationBOList.get(i).getFirstCategory());//一级类目
                        epgSectionService.updateById(epgSectionDTO);
                        //删除之前的商品
                        epgSectionCommodityRelationService.deleteBySectionId(id);
                    }
                    //频道下添加商品
                    for (int j = 0; j < epgDecorationBOList.get(i).getEpgDecorationBOList().size(); j++) {
                        EpgDecorationBO epgDecorationBO = epgDecorationBOList.get(i).getEpgDecorationBOList().get(j);
                        EpgSectionCommodityRelationSaveBO escr = new EpgSectionCommodityRelationSaveBO();
                        escr.setCommodityId(epgDecorationBO.getCommodityId());
                        escr.setSize(Integer.parseInt(epgDecorationBO.getSize()));
                        escr.setSectionId(id);
                        escr.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                        escr.setUrl(epgDecorationBO.getUrl());
                        escr.setSkipUrl(epgDecorationBO.getSkipUrl());
                        escr.setSort(j + 1);
                        epgSectionCommodityRelationService.add(escr);
                    }
                }
            }
            //卖家指南：sellGuide，买家指南：buyGuide，采购信息：purchase，金融服务：financial，大家都在看：everyoneWatching，友情链接：links
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_SELL_GUIDE.getMsg())) {//卖家指南
                setModuleContent(templetId, templetModuleId, ShoppingCodeEnum.DECORATE_SELL_GUIDE.getMsg(), map);
            }
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_BUY_GUIDE.getMsg())) {//买家指南
                setModuleContent(templetId, templetModuleId, ShoppingCodeEnum.DECORATE_BUY_GUIDE.getMsg(), map);
            }
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_PURCHASE.getMsg())) {//采购信息
                setModuleContent(templetId, templetModuleId, ShoppingCodeEnum.DECORATE_PURCHASE.getMsg(), map);
            }
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_FINANCIAL.getMsg())) {//金融服务
                setModuleContent(templetId, templetModuleId, ShoppingCodeEnum.DECORATE_FINANCIAL.getMsg(), map);
            }
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_EVERYINE_WATCHING.getMsg())) {//大家都在看
                setModuleContent(templetId, templetModuleId, ShoppingCodeEnum.DECORATE_EVERYINE_WATCHING.getMsg(), map);
            }
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_LINKS.getMsg())) {//友情链接
                setModuleContent(templetId, templetModuleId, ShoppingCodeEnum.DECORATE_LINKS.getMsg(), map);
            }
            if (StrUtils.isNotEmpty(templetModuleBO.getName())) {
                dto.setName(templetModuleBO.getName());
            }

            if (templetModuleBO.getIsPublish() != null) {
                dto.setIsPublish(templetModuleBO.getIsPublish().toString());
            }
            if (templetModuleBO.getSort() != null) {
                dto.setSort(templetModuleBO.getSort().toString());
            }
            if (templetModuleBO.getStatus() != null) {
                dto.setStatus(templetModuleBO.getStatus().toString());
            }
            templetModuleService.updateById(dto);
        }
        return new Result<>();
    }


    @Override
    public Result editPlatformFinancial(List<TempletModuleBO> list) {
        for (TempletModuleBO templetModuleBO : list) {
            Long templetModuleId = templetModuleBO.getId();
            Long templetId = templetModuleBO.getTempletId();

            TempletModuleDTO dto = templetModuleService.selectById(templetModuleId);
            if (dto == null) {
                return new Result(CodeEnum.FAIL_100002);
            }
            Map<String, String> map = templetModuleBO.getMap();
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_BANNER.getMsg())) {//banner处理
                setModuleContent(templetId, templetModuleId, ShoppingCodeEnum.DECORATE_BANNER.getMsg(), map);
            }

            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_BANKS.getMsg())) {
                //银行
                setModuleContent(templetId, templetModuleId, ShoppingCodeEnum.DECORATE_BANKS.getMsg(), map);
            }

            if (StrUtils.isNotEmpty(templetModuleBO.getName())) {
                dto.setName(templetModuleBO.getName());
            }

            if (templetModuleBO.getIsPublish() != null) {
                dto.setIsPublish(templetModuleBO.getIsPublish().toString());
            }
            if (templetModuleBO.getSort() != null) {
                dto.setSort(templetModuleBO.getSort().toString());
            }
            if (templetModuleBO.getStatus() != null) {
                dto.setStatus(templetModuleBO.getStatus().toString());
            }
            templetModuleService.updateById(dto);
        }
        return new Result();
    }

    @Transactional
    @Override
    public Result insertPlatformFinancial(List<TempletModuleBO> templetModuleBOList, String name, String skipUrl) {
        Long templetId = templetModuleBOList.get(0).getTempletId();
        //创建模板并返回id
        Long newTemplateId = ymlConfig.getPkId();
        TempletInfoDTO templetInfoDTO = new TempletInfoDTO();
        templetInfoDTO.setId(newTemplateId);
        templetInfoDTO.setType(ShoppingCodeEnum.TEMPLATE_TYPE_FINANCIAL.getCode());
        templetInfoDTO.setName(name);
        templetInfoDTO.setSkipUrl(pageShowUrl + "/home/Financialto?templateId=" + newTemplateId);
        this.insert(templetInfoDTO);

        //创建模板关联关系表
        TempletPlatformRelationSaveBO templetPlatformRelationSaveBO = new TempletPlatformRelationSaveBO();
        templetPlatformRelationSaveBO.setTempletId(newTemplateId);
        templetPlatformRelationSaveBO.setShopId(0L);
        templetPlatformRelationSaveBO.setType(ShoppingCodeEnum.TEMPLATE_TYPE_FINANCIAL.getCode());
        templetPlatformRelationSaveBO.setParentTemplateId(templetId);
        templetPlatformRelationService.add(templetPlatformRelationSaveBO);

        for (TempletModuleBO templetModuleBO : templetModuleBOList) {
            Map<String, String> map = templetModuleBO.getMap();
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_BANNER.getMsg())) {//banner处理
                TempletModuleDTO templetModuleDTO = new TempletModuleDTO();
                Long templetModuleId = ymlConfig.getPkId();
                templetModuleDTO.setId(templetModuleId);
                templetModuleDTO.setTempletId(newTemplateId);
                templetModuleDTO.setName("平台金融服务页banner");
                templetModuleDTO.setKey(ShoppingCodeEnum.DECORATE_BANNER.getMsg());
                templetModuleDTO.setSort(1 + "");
                templetModuleDTO.setNodes("平台金融服务页banner");
                templetModuleDTO.setModuleType(3);
                templetModuleService.insert(templetModuleDTO);
                setModuleContent(templetId, templetModuleId, ShoppingCodeEnum.DECORATE_BANNER.getMsg(), map);
            }

            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_BANKS.getMsg())) {
                //银行
                TempletModuleDTO templetModuleDTO = new TempletModuleDTO();
                Long templetModuleId = ymlConfig.getPkId();
                templetModuleDTO.setId(templetModuleId);
                templetModuleDTO.setTempletId(newTemplateId);
                templetModuleDTO.setName("平台金融服务页banks");
                templetModuleDTO.setKey(ShoppingCodeEnum.DECORATE_BANKS.getMsg());
                templetModuleDTO.setSort(2 + "");
                templetModuleDTO.setNodes("平台金融服务页banks");
                templetModuleDTO.setModuleType(3);
                templetModuleService.insert(templetModuleDTO);
                setModuleContent(templetId, templetModuleId, ShoppingCodeEnum.DECORATE_BANKS.getMsg(), map);
            }
        }
        return new Result();
    }

    @Override
    public Result delPlatformFinancial(Long id) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("templet_id", id);
        epgZtgfInfoService.delete(wrapper);
        //删除模板下面的模块
        templetModuleService.deleteByTemplateId(id);
        //删除模板关联表的数据
        templetPlatformRelationService.deleteByTemplateId(id);
        //删除模板
        deleteById(id);
        return new Result();
    }

    @Override
    public Result savePlatformFinancialDefault(Long id) {
        TempletPlatformRelationDTO tpdto = templetPlatformRelationService.getByTemplateId(id);
        Long parentTemplateId = tpdto.getParentTemplateId();
        List<TempletPlatformRelationDTO> templetPlatformRelationDTOList = templetPlatformRelationService.getByShopIdAngTyep(0L, 3, parentTemplateId);
        for (TempletPlatformRelationDTO templetPlatformRelationDTO : templetPlatformRelationDTOList) {
            if (templetPlatformRelationDTO.getTempletId().equals(id)) {
                templetPlatformRelationDTO.setIsDefault(ShoppingCodeEnum.IS_DEFAULT_TRUE.getCode());
            } else {
                templetPlatformRelationDTO.setIsDefault(ShoppingCodeEnum.IS_DEFAULT_FALSE.getCode());
            }

            templetPlatformRelationService.updateById(templetPlatformRelationDTO);

        }
        return new Result();
    }

    private void setModuleContent(Long templetId, Long templetModuleId, String key, Map<String, String> map) {
        //先删除之前的推荐专区
        epgZtgfInfoService.deleteByModuleId(templetModuleId);
        log.info("装修key:{}", key);
        String mapValue = map.get(key);
        log.info("装修value:{}", mapValue);
        List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(mapValue, EpgDecorationBO.class);
        log.info("装修list:{}", epgDecorationBOList);
        for (int i = 0; i < epgDecorationBOList.size(); i++) {
            EpgZtgfInfoSaveBO epgZtgfInfoSaveBO = new EpgZtgfInfoSaveBO();
            epgZtgfInfoSaveBO.setName(epgDecorationBOList.get(i).getName());
            epgZtgfInfoSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
            epgZtgfInfoSaveBO.setSkipUrl(epgDecorationBOList.get(i).getSkipUrl());
            epgZtgfInfoSaveBO.setType(epgDecorationBOList.get(i).getSkipType());
            epgZtgfInfoSaveBO.setModuleId(templetModuleId);
            epgZtgfInfoSaveBO.setTempletId(templetId);
            epgZtgfInfoSaveBO.setSort(i + 1);
            epgZtgfInfoSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
            epgZtgfInfoSaveBO.setIsTitle(epgDecorationBOList.get(i).getIsTitle());
            epgZtgfInfoSaveBO.setLeftBgColor(epgDecorationBOList.get(i).getLeftBgcolor());
            epgZtgfInfoSaveBO.setRightBgColor(epgDecorationBOList.get(i).getRightBgcolor());
            epgZtgfInfoService.add(epgZtgfInfoSaveBO);
        }
    }


    @Override
    public boolean add(TempletInfoSaveBO templetInfoSaveBO) {
        TempletInfoDTO templetInfoDTO = new TempletInfoDTO();
        BeanUtils.copyProperties(templetInfoSaveBO, templetInfoDTO);
        Long id = ymlConfig.getPkId();
        templetInfoDTO.setId(id);
        return this.insert(templetInfoDTO);
    }

    private Long addAndReturnId(TempletInfoSaveBO templetInfoSaveBO) {
        TempletInfoDTO templetInfoDTO = new TempletInfoDTO();
        BeanUtils.copyProperties(templetInfoSaveBO, templetInfoDTO);
        Long id = ymlConfig.getPkId();
        templetInfoDTO.setId(id);
        templetInfoDTO.setSkipUrl(pageShowUrl + "/home/gathear?templateId=" + id);
        this.insert(templetInfoDTO);
        return id;
    }

    /**
     * 删除有关联关系的
     *
     * @param id 模板id
     * @return
     */
    @Transactional
    @Override
    public boolean deletePlateActive(Long id) {
        //删除相关联的banner和分类
        epgAggregationBannerService.deleteByTemplateId(id);//删除banner
        List<EpgAggregationCategoryDTO> epgAggregationCategoryDTOList = epgAggregationCategoryService.getByTemplateId(id);
        for (EpgAggregationCategoryDTO epgAggregationCategoryDTO : epgAggregationCategoryDTOList) {
            epgAggregationCategoryCommodityRelationService.deleteByCategoryId(epgAggregationCategoryDTO.getId());//删除商品
            epgAggregationCategoryService.deleteByTemplate(id);//删除对应的分类
        }
        //删除模板下面的模块
        templetModuleService.deleteByTemplateId(id);
        TempletPlatformRelationDTO templetPlatformRelationDTO = templetPlatformRelationService.getByTemplateId(id);
        //删除模板关联表的数据
        templetPlatformRelationService.deleteByTemplateId(id);
        //删除模板
        deleteById(id);
        //如果是默认的活动页模板，还应该去除redis的缓存模板
        if (templetPlatformRelationDTO.getIsDefault() == 0) {
            redisUtil.deletePlateActiveInfo();
        }
        return true;
    }

    /**
     * 获取默认的平台首页所有功能模块
     *
     * @return
     */
    @Cacheable(cacheNames = "plateHomeTemInfoModule")
    @Override
    public List<TempletModuleDTO> getListPlatformTemInfoModule() {
        Long defaultPlateTemplateId = templetPlatformRelationService.getDefaultPlateActiveTemplateId(ShoppingCodeEnum.TEMPLATE_TYPE_HOME.getCode());
        if (null == defaultPlateTemplateId) {
            return null;
        }
        return getListTemplateModuleById(defaultPlateTemplateId);
    }

    /**
     * 获取默认的平台活动页所有功能模块
     *
     * @return
     */
    //@Cacheable(cacheNames = "plateActiveTemInfoModule")
    @Override
    public List<TempletModuleDTO> getListPlatformActiveTemInfoModule() {
        Object plateActiveInfo = redisUtil.getPlateActiveInfo();
        if (null != plateActiveInfo) {
            return (List<TempletModuleDTO>) plateActiveInfo;
        }
        Long defaultPlateActiveTemplateId = templetPlatformRelationService.getDefaultPlateActiveTemplateId(ShoppingCodeEnum.TEMPLATE_TYPE_ACTIVE.getCode());
        if (null == defaultPlateActiveTemplateId) {
            return null;
        }
        List<TempletModuleDTO> listTemplateModule = getListTemplateModuleById(defaultPlateActiveTemplateId);
        redisUtil.savePlateActiveInfo(listTemplateModule);//添加进redis
        return listTemplateModule;
    }

    /**
     * 根据店铺Id获取默认店铺首页所有功能信息
     *
     * @param shopId
     * @return
     */
    @Override
    public List<TempletModuleDTO> getDefaultListShopTemInfoModule(Long shopId) {
        ShopActiveRelationDTO defaultShopActive = shopActiveRelationService.getDefaultShopActive(shopId);
        if (null == defaultShopActive) {
            return null;
        }
        return getListTemplateModuleById(defaultShopActive.getShopTempletId());
    }

    /**
     * 根据店铺Id获取默认店铺活动页所有功能信息
     *
     * @param shopId
     * @return
     */
    @Override
    public List<TempletModuleDTO> getDefaultListShopActiveTemInfoModule(Long shopId) {
        ShopActiveRelationDTO defaultShopActive = shopActiveRelationService.getDefaultShopActive(shopId);
        if (null == defaultShopActive) {
            return null;
        }
        return getListTemplateModuleById(defaultShopActive.getActivityTempletId());
    }

    /**
     * 根据模板id 获取功能
     *
     * @param templateId
     * @return
     */
    @Override
    public List<TempletModuleDTO> getListTemplateModuleById(Long templateId) {
        TempletModuleBO templetModuleBO = new TempletModuleBO();
        templetModuleBO.setTempletId(templateId);
        return templetModuleService.getBySelectTemplatModuleInfoNoPage(templetModuleBO);//所有的功能模块
    }

    /**
     * 获取所有的平台活动页模板
     *
     * @return
     */
    @Override
    public List<TemplateInfoDVO> getListPlatformTemByType(int type) {
        List<TemplateInfoDVO> templetInfoDTOList = new ArrayList<>();
        List<TempletPlatformRelationDTO> templetPlatformRelationDTOList = templetPlatformRelationService.getByShopIdAngTyep(0L, type, 0L);
        for (TempletPlatformRelationDTO templetPlatformRelationDTO : templetPlatformRelationDTOList) {
            TempletInfoDTO templetInfoDTO = selectById(templetPlatformRelationDTO.getTempletId());
            TemplateInfoDVO templateInfoDVO = new TemplateInfoDVO();
            BeanUtils.copyProperties(templetInfoDTO, templateInfoDVO);
            templateInfoDVO.setIsDefault(templetPlatformRelationDTO.getIsDefault());
            templetInfoDTOList.add(templateInfoDVO);
        }
        return templetInfoDTOList;
    }

//    /**
//     * 根据店铺Id获取模板信息
//    * @param shopId
//     * @return
//     */
//    @Override
//    public List<TempletInfoDTO> getShopTemListByShopId(Long shopId) {
//        return templetPlatformRelationService.getTempletInfoListByShopId(shopId);
//    }


    /**
     * 根据店铺Id获取默认店铺首页模板id
     *
     * @param shopId
     * @return
     */
    public Long getDefaultTemIdByShopId(Long shopId) {
        ShopActiveRelationDTO defaultShopActive = shopActiveRelationService.getDefaultShopActive(shopId);
        if (null == defaultShopActive) {
            return Long.parseLong("0");
        }
        return defaultShopActive.getShopTempletId();
    }

    /**
     * 根据店铺Id获取默认店铺首页模板信息
     *
     * @param shopId
     * @return
     */
    @Override
    public TempletInfoVO getDefaultShopTemListByShopId(Long shopId) {
        ShopActiveRelationDTO defaultShopActive = shopActiveRelationService.getDefaultShopActive(shopId);
        if (null == defaultShopActive) {
            return null;
        }
        return getShopDecorationInfoByShopId(defaultShopActive.getShopTempletId(), shopId);
    }

    /**
     * 根据店铺Id获取默认店铺活动页模板信息
     *
     * @param shopId
     * @return
     */
    @Override
    public TempletInfoVO getDefaultShopActiveTemListByShopId(Long shopId) {
        ShopActiveRelationDTO defaultShopActive = shopActiveRelationService.getDefaultShopActive(shopId);
        if (null == defaultShopActive) {
            return null;
        }
        return getShopActiveTemInfo(defaultShopActive.getActivityTempletId(), shopId);
    }

    /**
     * 获取默认的平台首页信息
     *
     * @return
     */
    @Override
    public TempletInfoVO getPlatformTemInfo() {
        SysPlatformDTO defaultPlatform = sysPlatformService.getDefaultPlatform();
        return getPlatformInfoByTemId(defaultPlatform.getHomeTempletId());
    }

    /**
     * 平台首页发布
     *
     * @param templetModuleBOList
     * @return
     */
    @Transactional
    @Override
    public Result issuePlatformTemplate(List<TempletModuleBO> templetModuleBOList) {
        for (TempletModuleBO templetModuleBO : templetModuleBOList) {
            Long templetModuleId = templetModuleBO.getId();
            Long templetId = templetModuleBO.getTempletId();
            Map<String, String> map = templetModuleBO.getMap();
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_BANNER.getMsg())) {//banner处理
                //先删除之前的banner
                epgBannerService.deleteByModuleId(templetModuleId);
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_BANNER.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    EpgBannerSaveBO epgBannerSaveBO = new EpgBannerSaveBO();
                    epgBannerSaveBO.setBannerType(epgDecorationBOList.get(i).getBannerType());
                    epgBannerSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                    epgBannerSaveBO.setModuleId(templetModuleId);
                    epgBannerSaveBO.setSkipUrlPc(epgDecorationBOList.get(i).getSkipUrl());
                    epgBannerSaveBO.setSkipType(epgDecorationBOList.get(i).getSkipType());
                    epgBannerSaveBO.setTempletId(templetId);
                    epgBannerSaveBO.setSort(i + 1);
                    epgBannerSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgBannerSaveBO.setLeftBgcolor(epgDecorationBOList.get(i).getLeftBgcolor());
                    epgBannerSaveBO.setRightBgcolor(epgDecorationBOList.get(i).getRightBgcolor());
                    epgBannerService.add(epgBannerSaveBO);
                }
            }

            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_MAIN_NAVIGATION.getMsg())) {//主导航处理
                //先删除之前的主导航
                epgMainNavigationService.deleteByModuleId(templetModuleId);
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_MAIN_NAVIGATION.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    EpgMainNavigationSaveBO epgMainNavigationSaveBO = new EpgMainNavigationSaveBO();
                    epgMainNavigationSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                    epgMainNavigationSaveBO.setName(epgDecorationBOList.get(i).getName());
                    epgMainNavigationSaveBO.setSkipUrlPc(epgDecorationBOList.get(i).getSkipUrl());
                    epgMainNavigationSaveBO.setSkipType(epgDecorationBOList.get(i).getSkipType());
                    epgMainNavigationSaveBO.setModuleId(templetModuleId);
                    epgMainNavigationSaveBO.setTempletId(templetId);
                    epgMainNavigationSaveBO.setSort(i + 1);
                    epgMainNavigationSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgMainNavigationService.add(epgMainNavigationSaveBO);
                }
            }

            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_TOPIC.getMsg())) {//推荐专区
                //先删除之前的推荐专区
                epgTopicService.deleteByModuleId(templetModuleId);
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_TOPIC.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    EpgTopicSaveBO epgTopicSaveBO = new EpgTopicSaveBO();
                    epgTopicSaveBO.setSkipUrlPc(epgDecorationBOList.get(i).getSkipUrl());
                    epgTopicSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                    epgTopicSaveBO.setModuleId(templetModuleId);
                    epgTopicSaveBO.setTempletId(templetId);
                    epgTopicSaveBO.setSort(i + 1);
                    epgTopicSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgTopicService.add(epgTopicSaveBO);
                }
            }

            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_SHOW_WINDOW.getMsg())) {//新鲜好物
                //先删除之前的新鲜好物
                epgShowWindowService.deleteByModuleId(templetModuleId);
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_SHOW_WINDOW.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    EpgShowWindowSaveBO epgShowWindowSaveBO = new EpgShowWindowSaveBO();
                    epgShowWindowSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                    epgShowWindowSaveBO.setCommodityId(epgDecorationBOList.get(i).getCommodityId());
                    epgShowWindowSaveBO.setModuleId(templetModuleId);
                    epgShowWindowSaveBO.setTempletId(templetId);
                    epgShowWindowSaveBO.setSort(i + 1);
                    epgShowWindowSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgShowWindowService.add(epgShowWindowSaveBO);
                }
            }

            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_ZTGF_INFO.getMsg())) {//中铁工服
                //先删除之前的中铁工服信息
                epgZtgfInfoService.deleteByModuleId(templetModuleId);
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_ZTGF_INFO.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    EpgZtgfInfoSaveBO epgZtgfInfoSaveBO = new EpgZtgfInfoSaveBO();
                    epgZtgfInfoSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                    epgZtgfInfoSaveBO.setSkipUrl(epgDecorationBOList.get(i).getSkipUrl());
                    epgZtgfInfoSaveBO.setType(epgDecorationBOList.get(i).getSkipType());
                    epgZtgfInfoSaveBO.setModuleId(templetModuleId);
                    epgZtgfInfoSaveBO.setTempletId(templetId);
                    epgZtgfInfoSaveBO.setSort(i + 1);
                    epgZtgfInfoSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgZtgfInfoService.add(epgZtgfInfoSaveBO);
                }
            }

            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_SECTION.getMsg())) {//频道
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_SECTION.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    Long id = epgDecorationBOList.get(i).getId();
                    if (null == id) {
                        EpgSectionSaveBO epgSectionSaveBO = new EpgSectionSaveBO();
                        epgSectionSaveBO.setModuleId(templetModuleId);
                        epgSectionSaveBO.setTempletId(templetId);
                        epgSectionSaveBO.setSort(i + 1);
                        epgSectionSaveBO.setName(epgDecorationBOList.get(i).getName());
                        epgSectionSaveBO.setNote(epgDecorationBOList.get(i).getContent());
                        epgSectionSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                        epgSectionSaveBO.setKey(epgDecorationBOList.get(i).getKey());
                        epgSectionSaveBO.setFirstCategory(epgDecorationBOList.get(i).getFirstCategory());
                        epgSectionSaveBO.setZeroCategory(epgDecorationBOList.get(i).getZeroCategory());
                        id = epgSectionService.addAndReturnId(epgSectionSaveBO);
                    } else {
                        EpgSectionDTO epgSectionDTO = epgSectionService.selectById(id);
                        epgSectionDTO.setName(epgDecorationBOList.get(i).getName());
                        epgSectionDTO.setZeroCategory(epgDecorationBOList.get(i).getZeroCategory());
                        epgSectionDTO.setFirstCategory(epgDecorationBOList.get(i).getFirstCategory());//一级类目
                        epgSectionService.updateById(epgSectionDTO);
                        //删除之前的商品
                        epgSectionCommodityRelationService.deleteBySectionId(id);
                    }
                    //频道下添加商品
                    for (int j = 0; j < epgDecorationBOList.get(i).getEpgDecorationBOList().size(); j++) {
                        EpgDecorationBO epgDecorationBO = epgDecorationBOList.get(i).getEpgDecorationBOList().get(j);
                        EpgSectionCommodityRelationSaveBO escr = new EpgSectionCommodityRelationSaveBO();
                        escr.setCommodityId(epgDecorationBO.getCommodityId());
                        escr.setSize(Integer.parseInt(epgDecorationBO.getSize()));
                        escr.setSectionId(id);
                        escr.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                        escr.setUrl(epgDecorationBO.getUrl());
                        escr.setSkipUrl(epgDecorationBO.getSkipUrl());
                        escr.setSort(j + 1);
                        epgSectionCommodityRelationService.add(escr);
                    }
                }
            }
        }
        return new Result();
    }

    /**
     * @return : boolean
     * @Author :chenyu.wang
     * @Description : 检查跳转链接是否合规
     * @Date : 14:54 2021.01.13
     * @Param : [moduleBO]
     **/
    private boolean checkHttp(TempletModuleBO moduleBO) {
        // 验证banner
        if (moduleBO.getKey().equals(ShoppingCodeEnum.DECORATE_BANNER.getMsg())) {
            List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(moduleBO.getMap().get(ShoppingCodeEnum.DECORATE_BANNER.getMsg()), EpgDecorationBO.class);
            if (CustomizeUtil.listIsNotNull(epgDecorationBOList)) {
                for (EpgDecorationBO epgDecorationBO : epgDecorationBOList) {
                    if (CustomizeUtil.strIsNotNull(epgDecorationBO.getSkipUrl())) {
                        if (!epgDecorationBO.getSkipUrl().contains("ztgfmall.com")) {
                            return false;
                        }
                    }
                }
            }
        }
        // 验证主导航
        if (moduleBO.getKey().equals(ShoppingCodeEnum.DECORATE_MAIN_NAVIGATION.getMsg())) {
            List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(moduleBO.getMap().get(ShoppingCodeEnum.DECORATE_MAIN_NAVIGATION.getMsg()), EpgDecorationBO.class);
            if (CustomizeUtil.listIsNotNull(epgDecorationBOList)) {
                for (EpgDecorationBO bo : epgDecorationBOList) {
                    if (CustomizeUtil.strIsNotNull(bo.getSkipUrl())) {
                        if (!bo.getSkipUrl().contains("ztgfmall.com")) {
                            return false;
                        }
                    }
                }
            }
        }
        // 验证频道
        if (moduleBO.getKey().equals(ShoppingCodeEnum.DECORATE_SECTION.getMsg())) {
            List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(moduleBO.getMap().get(ShoppingCodeEnum.DECORATE_SECTION.getMsg()), EpgDecorationBO.class);
            if (CustomizeUtil.listIsNotNull(epgDecorationBOList)) {
                for (EpgDecorationBO bo : epgDecorationBOList) {
                    if (CustomizeUtil.listIsNotNull(bo.getEpgDecorationBOList())) {
                        for (EpgDecorationBO decorationBO : bo.getEpgDecorationBOList()) {
                            if (CustomizeUtil.strIsNotNull(decorationBO.getSkipUrl())) {
                                if (!decorationBO.getSkipUrl().contains("ztgfmall.com")) {
                                    return false;
                                }
                            }
                        }
                    }
                }
            }
        }
        return true;
    }

    /**
     * 店铺发布
     *
     * @param templetModuleBOList
     * @return
     */
    @Transactional
    @Override
    public Result issueShopTemplate(List<TempletModuleBO> templetModuleBOList) {
        Result<?> result = new Result<>();
        for (TempletModuleBO templetModuleBO : templetModuleBOList) {
            // 验证跳转地址
            boolean checkHttp = this.checkHttp(templetModuleBO);
            if (!checkHttp) {
                result.setCode(CodeEnum.RENOVATION_FAIL.getCode());
                result.setMsg(CodeEnum.RENOVATION_FAIL.getMsg());
                return result;
            }
            Long templetModuleId = templetModuleBO.getId();
            Long templetId = templetModuleBO.getTempletId();
            Map<String, String> map = templetModuleBO.getMap();
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_BANNER.getMsg())) {//banner处理
                //先删除之前的banner
                epgShopBannerService.deleteByModuleId(templetModuleId);
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_BANNER.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    EpgShopBannerSaveBO epgShopBannerSaveBO = new EpgShopBannerSaveBO();
                    epgShopBannerSaveBO.setBannerType(epgDecorationBOList.get(i).getBannerType());
                    epgShopBannerSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                    epgShopBannerSaveBO.setModuleId(templetModuleId);
                    epgShopBannerSaveBO.setSkipUrlPc(epgDecorationBOList.get(i).getSkipUrl());
                    epgShopBannerSaveBO.setTempletId(templetId);
                    epgShopBannerSaveBO.setSort(i + 1);
                    epgShopBannerSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgShopBannerSaveBO.setShopId(epgDecorationBOList.get(i).getShopId());
                    epgShopBannerService.add(epgShopBannerSaveBO);
                }
            }

            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_SHOP_INFO.getMsg())) {//店铺信息处理
                EpgDecorationBO epgDecorationBO = JSON.parseObject(map.get(ShoppingCodeEnum.DECORATE_SHOP_INFO.getMsg()), EpgDecorationBO.class);
//                List<EpgDecorationBO> epgDecorationBOList =  JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_SHOP_INFO.getMsg()),EpgDecorationBO.class);
//                for (int i = 0; i < epgDecorationBOList.size(); i++) {
//                    ShopInfoDTO shopInfoDTO = shopInfoService.selectById(epgDecorationBOList.get(i).getShopId());
//                    shopInfoDTO.setLogo(epgDecorationBOList.get(i).getUrl());
//                    shopInfoDTO.setHighlights(epgDecorationBOList.get(i).getName());
//                    shopInfoDTO.setNotes(epgDecorationBOList.get(i).getNote());
//                    shopInfoDTO.setDetails(epgDecorationBOList.get(i).getContent());
//                    shopInfoService.updateById(shopInfoDTO);//更新店铺信息
//                }

                ShopInfoDTO shopInfoDTO = shopInfoService.selectById(epgDecorationBO.getShopId());
                shopInfoDTO.setLogo(epgDecorationBO.getUrl());
                shopInfoDTO.setHighlights(epgDecorationBO.getName());
                shopInfoDTO.setNotes(epgDecorationBO.getNote());
                shopInfoDTO.setDetails(epgDecorationBO.getContent());
                shopInfoService.updateById(shopInfoDTO);//更新店铺信息
                // TODO 更新店铺ES
                this.updateEsShop(shopInfoDTO.getId());
            }

            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_MAIN_NAVIGATION.getMsg())) {//主导航处理
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_MAIN_NAVIGATION.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    EpgShopMainNavigationSaveBO epgShopMainNavigationSaveBO = new EpgShopMainNavigationSaveBO();
                    epgShopMainNavigationSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                    epgShopMainNavigationSaveBO.setName(epgDecorationBOList.get(i).getName());
                    epgShopMainNavigationSaveBO.setSkipUrlPc(epgDecorationBOList.get(i).getSkipUrl());
                    epgShopMainNavigationSaveBO.setModuleId(templetModuleId);
                    epgShopMainNavigationSaveBO.setTempletId(templetId);
                    epgShopMainNavigationSaveBO.setSort(i + 1);
                    epgShopMainNavigationSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgShopMainNavigationSaveBO.setShopId(epgDecorationBOList.get(i).getShopId());
                    epgShopMainNavigationService.add(epgShopMainNavigationSaveBO);
                }
            }

            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_SECTION.getMsg())) {//频道
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_SECTION.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    Long id = epgDecorationBOList.get(i).getId();
                    if (null == id) {
                        EpgShopSectionSaveBO epgShopSectionSaveBO = new EpgShopSectionSaveBO();
                        epgShopSectionSaveBO.setModuleId(templetModuleId);
                        epgShopSectionSaveBO.setTempletId(templetId);
                        epgShopSectionSaveBO.setSort(i + 1);
                        epgShopSectionSaveBO.setName(epgDecorationBOList.get(i).getName());
                        epgShopSectionSaveBO.setNote(epgDecorationBOList.get(i).getContent());
                        epgShopSectionSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                        epgShopSectionSaveBO.setShopId(epgDecorationBOList.get(i).getShopId());
                        epgShopSectionSaveBO.setKey(epgDecorationBOList.get(i).getKey());
                        id = epgShopSectionService.addAndReturnId(epgShopSectionSaveBO);
                    } else {
                        EpgShopSectionDTO epgShopSectionDTO = epgShopSectionService.selectById(id);
                        epgShopSectionDTO.setName(epgDecorationBOList.get(i).getName());
                        epgShopSectionService.updateById(epgShopSectionDTO);
                        //删除之前的商品
                        epgShopSectionCommodityRelationService.deleteBySectionId(id);
                    }
                    HashSet<Long> set = new HashSet<>();
                    //频道下添加商品
                    for (int j = 0; j < epgDecorationBOList.get(i).getEpgDecorationBOList().size(); j++) {
                        EpgDecorationBO epgDecorationBO = epgDecorationBOList.get(i).getEpgDecorationBOList().get(j);
                        Long commodityId = epgDecorationBO.getCommodityId();
                        boolean add = set.add(commodityId);
                        if (!add) {//不能添加重复商品
                            continue;
                        }
                        EpgShopSectionCommodityRelationSaveBO esscr = new EpgShopSectionCommodityRelationSaveBO();
                        esscr.setCommodityId(epgDecorationBO.getCommodityId());
                        esscr.setSize(Integer.parseInt(epgDecorationBO.getSize()));
                        esscr.setSectionId(id);
                        esscr.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                        esscr.setUrl(epgDecorationBO.getUrl());
                        esscr.setSkipUrl(epgDecorationBO.getSkipUrl());
                        esscr.setSort(j + 1);
                        epgShopSectionCommodityRelationService.add(esscr);
                    }
                }
            }
        }
        return new Result();
    }

    /**
     * 平台活动页新发布
     *
     * @param templetModuleBOList
     * @return
     */
    @Transactional
    @Override
    public Result createActiveTemplate(List<TempletModuleBO> templetModuleBOList, String name, String skipUrl) {
        Long templetId = templetModuleBOList.get(0).getTempletId();
        //创建模板并返回id
        TempletInfoSaveBO templetInfoSaveBO = new TempletInfoSaveBO();
        templetInfoSaveBO.setType(ShoppingCodeEnum.TEMPLATE_TYPE_ACTIVE.getCode());
        templetInfoSaveBO.setName(name);
        Long newTemplateId = createDefaultPlateActiveTemplate(templetInfoSaveBO, templetId);

        for (TempletModuleBO templetModuleBO : templetModuleBOList) {
            Map<String, String> map = templetModuleBO.getMap();
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_BANNER.getMsg())) {//banner处理
                Long templetModuleId = templetModuleService.createBannerModule(newTemplateId);//存放返回的banner功能id
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_BANNER.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    EpgAggregationBannerSaveBO epgAggregationBannerSaveBO = new EpgAggregationBannerSaveBO();
                    epgAggregationBannerSaveBO.setBannerType(0);
                    epgAggregationBannerSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                    epgAggregationBannerSaveBO.setSkipUrlPc(epgDecorationBOList.get(i).getSkipUrl());
                    epgAggregationBannerSaveBO.setModuleId(templetModuleId);
                    epgAggregationBannerSaveBO.setTempletId(newTemplateId);
                    epgAggregationBannerSaveBO.setSort(i + 1);
                    epgAggregationBannerSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgAggregationBannerSaveBO.setShopId(epgDecorationBOList.get(i).getShopId());
                    epgAggregationBannerSaveBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
                    epgAggregationBannerService.add(epgAggregationBannerSaveBO);
                }
            }

            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_SECTION.getMsg())) {//分类频道
                Long templetModuleId = templetModuleService.createSectionModule(newTemplateId);//存放返回的频道功能id
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_SECTION.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    String sectionName = epgDecorationBOList.get(i).getName();
                    EpgAggregationCategorySaveBO epgAggregationCategorySaveBO = new EpgAggregationCategorySaveBO();
                    epgAggregationCategorySaveBO.setModuleId(templetModuleId);
                    epgAggregationCategorySaveBO.setTempletId(newTemplateId);
                    epgAggregationCategorySaveBO.setSort(i + 1);
                    epgAggregationCategorySaveBO.setName(sectionName);
                    epgAggregationCategorySaveBO.setNote(epgDecorationBOList.get(i).getContent());
                    epgAggregationCategorySaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                    epgAggregationCategorySaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgAggregationCategorySaveBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
                    Long categoryId = epgAggregationCategoryService.addAndReturnId(epgAggregationCategorySaveBO);
                    //分类下添加商品
                    HashSet<Long> set = new HashSet<>();
                    for (int j = 0; j < epgDecorationBOList.get(i).getEpgDecorationBOList().size(); j++) {
                        EpgDecorationBO epgDecorationBO = epgDecorationBOList.get(i).getEpgDecorationBOList().get(j);
                        Long commodityId = epgDecorationBO.getCommodityId();
                        boolean add = set.add(commodityId);
                        if (!add) {//不能添加重复商品
                            continue;
                        }
                        EpgAggregationCategoryCommodityRelationSaveBO epgAggregationCategoryCommodityRelationSaveBO = new EpgAggregationCategoryCommodityRelationSaveBO();
                        epgAggregationCategoryCommodityRelationSaveBO.setCommodityId(commodityId);
                        epgAggregationCategoryCommodityRelationSaveBO.setSize(Integer.parseInt(epgDecorationBO.getSize()));
                        epgAggregationCategoryCommodityRelationSaveBO.setCategoryId(categoryId);
                        epgAggregationCategoryCommodityRelationSaveBO.setSort(j + 1);
                        epgAggregationCategoryCommodityRelationSaveBO.setUrl(epgDecorationBO.getUrl());
                        epgAggregationCategoryCommodityRelationService.add(epgAggregationCategoryCommodityRelationSaveBO);
                    }
                }
            }
        }
        return new Result();
    }

    /**
     * 活动页装修发布
     *
     * @param templetModuleBOList
     * @return
     */
    @Transactional
    @Override
    public Result issueActiveTemplate(List<TempletModuleBO> templetModuleBOList, String name) {
        Long tempTemplateId = 0L;
        for (TempletModuleBO templetModuleBO : templetModuleBOList) {
            Long templetId = templetModuleBO.getTempletId();
            tempTemplateId = templetId;
            Long templetModuleId = templetModuleBO.getId();
            Map<String, String> map = templetModuleBO.getMap();
            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_BANNER.getMsg())) {//banner处理
                //先删除之前的banner
                epgAggregationBannerService.deleteByModuleId(templetModuleId);
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_BANNER.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    EpgAggregationBannerSaveBO epgAggregationBannerSaveBO = new EpgAggregationBannerSaveBO();
                    epgAggregationBannerSaveBO.setBannerType(0);
                    epgAggregationBannerSaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                    epgAggregationBannerSaveBO.setSkipUrlPc(epgDecorationBOList.get(i).getSkipUrl());
                    epgAggregationBannerSaveBO.setModuleId(templetModuleId);
                    epgAggregationBannerSaveBO.setTempletId(templetId);
                    epgAggregationBannerSaveBO.setSort(i + 1);
                    epgAggregationBannerSaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgAggregationBannerSaveBO.setShopId(epgDecorationBOList.get(i).getShopId());
                    epgAggregationBannerSaveBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
                    epgAggregationBannerService.add(epgAggregationBannerSaveBO);
                }
            }

            if (templetModuleBO.getKey().equals(ShoppingCodeEnum.DECORATE_SECTION.getMsg())) {//分类频道
                List<EpgDecorationBO> epgDecorationBOList = JSON.parseArray(map.get(ShoppingCodeEnum.DECORATE_SECTION.getMsg()), EpgDecorationBO.class);
                for (int i = 0; i < epgDecorationBOList.size(); i++) {
                    Long categoryId = epgDecorationBOList.get(i).getId();
                    if (null == categoryId) {//表示新增
                        EpgAggregationCategorySaveBO epgAggregationCategorySaveBO = new EpgAggregationCategorySaveBO();
                        epgAggregationCategorySaveBO.setModuleId(templetModuleId);
                        epgAggregationCategorySaveBO.setTempletId(templetId);
                        epgAggregationCategorySaveBO.setSort(i + 1);
                        epgAggregationCategorySaveBO.setName(epgDecorationBOList.get(i).getName());
                        epgAggregationCategorySaveBO.setNote(epgDecorationBOList.get(i).getContent());
                        epgAggregationCategorySaveBO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                        epgAggregationCategorySaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                        epgAggregationCategorySaveBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
                        categoryId = epgAggregationCategoryService.addAndReturnId(epgAggregationCategorySaveBO);
                    } else {
                        EpgAggregationCategoryDTO epgAggregationCategoryDTO = epgAggregationCategoryService.selectById(categoryId);
                        epgAggregationCategoryDTO.setName(epgDecorationBOList.get(i).getName());
                        epgAggregationCategoryDTO.setNote(epgDecorationBOList.get(i).getNote());
                        epgAggregationCategoryDTO.setLogoUrl(epgDecorationBOList.get(i).getUrl());
                        epgAggregationCategoryDTO.setSort(i + 1);
                        epgAggregationCategoryService.updateById(epgAggregationCategoryDTO);
                        //添加之前删除商品
                        epgAggregationCategoryCommodityRelationService.deleteByCategoryId(categoryId);
                    }
                    HashSet<Long> set = new HashSet<>();
                    //分类下添加商品
                    for (int j = 0; j < epgDecorationBOList.get(i).getEpgDecorationBOList().size(); j++) {
                        EpgDecorationBO epgDecorationBO = epgDecorationBOList.get(i).getEpgDecorationBOList().get(j);
                        Long commodityId = epgDecorationBO.getCommodityId();
                        boolean add = set.add(commodityId);
                        if (!add) {//不能添加重复商品
                            continue;
                        }
                        EpgAggregationCategoryCommodityRelationSaveBO epgAggregationCategoryCommodityRelationSaveBO = new EpgAggregationCategoryCommodityRelationSaveBO();
                        epgAggregationCategoryCommodityRelationSaveBO.setCommodityId(commodityId);
                        epgAggregationCategoryCommodityRelationSaveBO.setSize(Integer.parseInt(epgDecorationBO.getSize()));
                        epgAggregationCategoryCommodityRelationSaveBO.setCategoryId(categoryId);
                        epgAggregationCategoryCommodityRelationSaveBO.setSort(j + 1);
                        epgAggregationCategoryCommodityRelationSaveBO.setUrl(epgDecorationBO.getUrl());
                        epgAggregationCategoryCommodityRelationService.add(epgAggregationCategoryCommodityRelationSaveBO);
                    }
                }
            }
        }
        if (StrUtils.isNotEmpty(name)) {
            TempletInfoDTO templetInfoDTO = selectById(tempTemplateId);
            if (null != templetInfoDTO) {
                templetInfoDTO.setName(name);
                updateById(templetInfoDTO);//更新模板名称
            }
        }
        return new Result();
    }

    /**
     * 获取默认的平台活动页信息
     *
     * @return
     */
    @Override
    public TempletInfoVO getPlatformActiveTemInfo() {
        Long defaultPlateActiveTemplateId = templetPlatformRelationService.getDefaultPlateActiveTemplateId(ShoppingCodeEnum.TEMPLATE_TYPE_ACTIVE.getCode());
        if (null == defaultPlateActiveTemplateId) {
            return null;
        }
        return getPActiveInfoByTemId(defaultPlateActiveTemplateId);
    }


    /**
     * 平台模板
     *
     * @param templateId 模板Id
     * @return
     */
    @Override
    public TempletInfoVO getPlatformInfoByTemId(Long templateId) {
        TempletModuleBO templetModuleBO = new TempletModuleBO();
        templetModuleBO.setTempletId(templateId);
        List<TempletModuleDTO> templetModuleDTOList = templetModuleService.getBySelectTemplatModuleInfoNoPage(templetModuleBO);//所有的功能模块
        TempletInfoDTO templetInfoDTO = selectById(templateId);//当前模板信息
        TempletInfoVO templetInfoVO = new TempletInfoVO();
        BeanUtils.copyProperties(templetInfoDTO, templetInfoVO);
        templetInfoVO.setSortList(new ArrayList<>());

        List<TempletModuleVO> templetModuleVOList = new ArrayList<>();
        for (TempletModuleDTO templetModuleDTO : templetModuleDTOList) {
            TempletModuleVO templetModuleVO = new TempletModuleVO();
            BeanUtils.copyProperties(templetModuleDTO, templetModuleVO);
            Long temModuleId = templetModuleVO.getId();//模板功能Id

            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_BANNER.getMsg())) {//banner
                EpgBannerBO epgBannerBO = new EpgBannerBO();
                epgBannerBO.setTempletId(templateId);
                epgBannerBO.setModuleId(temModuleId);
                List<EpgBannerDTO> epgBannerDTOList = epgBannerService.getBySelectBannerInfoNoPage(epgBannerBO);
                //templetModuleVO.setEpgBannerDTOList(epgBannerDTOList);//Banner添加进功能
                templetModuleVO.getMap().put(templetModuleVO.getKey(), epgBannerDTOList);
                templetInfoVO.getSortList().add(ShoppingCodeEnum.DECORATE_BANNER.getCode());
                templetModuleVOList.add(templetModuleVO);//添加功能集合
                continue;
            }

            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_MAIN_NAVIGATION.getMsg())) {//主导航
                EpgMainNavigationBO epgMainNavigationBO = new EpgMainNavigationBO();
                epgMainNavigationBO.setTempletId(templateId);
                epgMainNavigationBO.setModuleId(temModuleId);
                List<EpgMainNavigationDTO> epgMainNavigationDTOList = epgMainNavigationService.getBySelectMainNavigationInfoNoPage(epgMainNavigationBO);
                //templetModuleVO.setEpgMainNavigationDTOList(epgMainNavigationDTOList);//店铺主导航添加进功能
                templetModuleVO.getMap().put(templetModuleVO.getKey(), epgMainNavigationDTOList);
                templetInfoVO.getSortList().add(ShoppingCodeEnum.DECORATE_MAIN_NAVIGATION.getCode());
                templetModuleVOList.add(templetModuleVO);//添加功能集合
                continue;
            }

            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_SECTION.getMsg())) {//频道
                EpgSectionBO epgSectionBO = new EpgSectionBO();
                epgSectionBO.setTempletId(templateId);
                epgSectionBO.setModuleId(temModuleId);
                List<EpgSectionDTO> epgSectionDTOList = epgSectionService.getBySelectSectionInfoNoPage(epgSectionBO);//所有频道
                List<EpgSectionVO> epgSectionVOList = new ArrayList<>();
                for (EpgSectionDTO epgSectionDTO : epgSectionDTOList) {//频道下商品添加
                    EpgSectionCommodityRelationBO epgSectionCommodityRelationBO = new EpgSectionCommodityRelationBO();
                    epgSectionCommodityRelationBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgSectionCommodityRelationBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
                    epgSectionCommodityRelationBO.setSectionId(epgSectionDTO.getId());
                    List<EpgSectionCommodityRelationVO> epgSectionCommodityRelationVOList = epgSectionCommodityRelationService.getBySelectSectionCommodityRelationInfo(epgSectionCommodityRelationBO);
                    EpgSectionVO epgSectionVO = new EpgSectionVO();
                    BeanUtils.copyProperties(epgSectionDTO, epgSectionVO);
                    epgSectionVO.setEpgSectionCommodityRelationVOList(epgSectionCommodityRelationVOList);
                    epgSectionVOList.add(epgSectionVO);//添加频道栏目
                }
                //templetModuleVO.setEpgSectionVOList(epgSectionVOList);
                templetModuleVO.getMap().put(templetModuleVO.getKey(), epgSectionVOList);
                templetInfoVO.getSortList().add(ShoppingCodeEnum.DECORATE_SECTION.getCode());
                templetModuleVOList.add(templetModuleVO);//添加功能集合
                continue;
            }

            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_TOPIC.getMsg())) {//推荐专区
                EpgTopicBO epgTopicBO = new EpgTopicBO();
                epgTopicBO.setTempletId(templateId);
                epgTopicBO.setModuleId(temModuleId);
                List<EpgTopicVO> epgTopicVOList = epgTopicService.getBySelectTopicInfoNoPage(epgTopicBO);
                //templetModuleVO.setEpgTopicVOList(epgTopicVOList);//推荐专区添加进功能
                templetModuleVO.getMap().put(templetModuleVO.getKey(), epgTopicVOList);
                templetInfoVO.getSortList().add(ShoppingCodeEnum.DECORATE_TOPIC.getCode());
                templetModuleVOList.add(templetModuleVO);//添加功能集合
                continue;
            }

            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_SHOW_WINDOW.getMsg())) {//新鲜好物
                EpgShowWindowBO epgShowWindowBO = new EpgShowWindowBO();
                epgShowWindowBO.setTempletId(templateId);
                epgShowWindowBO.setModuleId(temModuleId);
                List<EpgShowWindowVO> epgShowWindowVOList = epgShowWindowService.getBySelectShowWindowInfoNoPage(epgShowWindowBO);
                //templetModuleVO.setEpgShowWindowVOList(epgShowWindowVOList);//新鲜好物添加进功能
                templetModuleVO.getMap().put(templetModuleVO.getKey(), epgShowWindowVOList);
                templetInfoVO.getSortList().add(ShoppingCodeEnum.DECORATE_SHOW_WINDOW.getCode());
                templetModuleVOList.add(templetModuleVO);//添加功能集合
                continue;
            }

            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_ZTGF_INFO.getMsg())) {//中铁信息
                EpgZtgfInfoBO epgZtgfInfoBO = new EpgZtgfInfoBO();
                epgZtgfInfoBO.setTempletId(templateId);
                epgZtgfInfoBO.setModuleId(temModuleId);
                List<EpgZtgfInfoDTO> epgZtgfInfoDTOList = epgZtgfInfoService.getBySelectZtgfInfoNoPage(epgZtgfInfoBO);
                //templetModuleVO.setEpgZtgfInfoDTOList(epgZtgfInfoDTOList);//中铁工服信息配置添加进功能
                templetModuleVO.getMap().put(templetModuleVO.getKey(), epgZtgfInfoDTOList);
                templetInfoVO.getSortList().add(ShoppingCodeEnum.DECORATE_ZTGF_INFO.getCode());
                templetModuleVOList.add(templetModuleVO);//添加功能集合
            }
        }
        templetInfoVO.setTempletModuleVOList(templetModuleVOList);//添加所有的已经启用模块信息并且排好序
        return templetInfoVO;
    }

    /**
     * 平台活动页
     *
     * @param templateId 模板Id
     * @return
     */
    public TempletInfoVO getPActiveInfoByTemId(Long templateId) {
        TempletModuleBO templetModuleBO = new TempletModuleBO();
        templetModuleBO.setTempletId(templateId);
        List<TempletModuleDTO> templetModuleDTOList = templetModuleService.getBySelectTemplatModuleInfoNoPage(templetModuleBO);//所有的功能模块
        TempletInfoDTO templetInfoDTO = selectById(templateId);//当前模板信息
        TempletInfoVO templetInfoVO = new TempletInfoVO();
        BeanUtils.copyProperties(templetInfoDTO, templetInfoVO);

        List<TempletModuleVO> templetModuleVOList = new ArrayList<>();
        for (TempletModuleDTO templetModuleDTO : templetModuleDTOList) {
            TempletModuleVO templetModuleVO = new TempletModuleVO();
            BeanUtils.copyProperties(templetModuleDTO, templetModuleVO);
            Long temModuleId = templetModuleVO.getId();//模板功能Id

            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_BANNER.getMsg())) {//banner
                EpgAggregationBannerBO epgAggregationBannerBO = new EpgAggregationBannerBO();
                epgAggregationBannerBO.setTempletId(templateId);
                epgAggregationBannerBO.setModuleId(temModuleId);
                List<EpgAggregationBannerDTO> epgAggregationBannerDTOList = epgAggregationBannerService.getBySelectBannerInfoNoPage(epgAggregationBannerBO);
                //templetModuleVO.setEpgBannerDTOList(epgBannerDTOList);//Banner添加进功能
                templetModuleVO.getMap().put(templetModuleVO.getKey(), epgAggregationBannerDTOList);
                templetModuleVOList.add(templetModuleVO);//添加功能集合
                continue;
            }

            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_CATEGORY_INFO.getMsg())) {//分类
                EpgAggregationCategoryBO epgAggregationCategoryBO = new EpgAggregationCategoryBO();
                epgAggregationCategoryBO.setTempletId(templateId);
                epgAggregationCategoryBO.setModuleId(temModuleId);
                List<EpgAggregationCategoryDTO> epgAggregationCategoryDTOList = epgAggregationCategoryService.getBySelectCategoryInfoNoPage(epgAggregationCategoryBO);//所有分类
                List<EpgAggregationCategoryVO> epgAggregationCategoryVOList = new ArrayList<>();
                for (EpgAggregationCategoryDTO epgAggregationCategoryDTO : epgAggregationCategoryDTOList) {//分类商品添加
                    EpgAggregationCategoryCommodityRelationBO epgAggregationCategoryCommodityRelationBO = new EpgAggregationCategoryCommodityRelationBO();
                    epgAggregationCategoryCommodityRelationBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgAggregationCategoryCommodityRelationBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
                    epgAggregationCategoryCommodityRelationBO.setCategoryId(epgAggregationCategoryDTO.getId());
                    List<EpgAggregationCategoryCommodityRelationVO> epgAggregationCategoryCommodityRelationVOList = epgAggregationCategoryCommodityRelationService.getBySelectSectionCommodityRelationInfo(epgAggregationCategoryCommodityRelationBO);
                    EpgAggregationCategoryVO epgAggregationCategoryVO = new EpgAggregationCategoryVO();
                    BeanUtils.copyProperties(epgAggregationCategoryDTO, epgAggregationCategoryVO);
                    epgAggregationCategoryVO.setEpgAggregationCategoryCommodityRelationVOList(epgAggregationCategoryCommodityRelationVOList);
                    epgAggregationCategoryVOList.add(epgAggregationCategoryVO);//添加频道栏目
                }
                //templetModuleVO.setEpgSectionVOList(epgSectionVOList);
                templetModuleVO.getMap().put(templetModuleVO.getKey(), epgAggregationCategoryVOList);
                templetModuleVOList.add(templetModuleVO);//添加功能集合
            }
        }
        templetInfoVO.setTempletModuleVOList(templetModuleVOList);//添加所有的已经启用模块信息并且排好序
        return templetInfoVO;
    }

    /**
     * 默认的店铺主页
     *
     * @param templateId
     * @return
     */
    @Override
    public TempletInfoVO getShopDecorationInfoByShopId(Long templateId, Long shopId) {
        TempletModuleBO templetModuleBO = new TempletModuleBO();
        templetModuleBO.setTempletId(templateId);
        TempletInfoDTO templetInfoDTO = selectById(templateId);//当前模板信息
        TempletInfoVO templetInfoVO = new TempletInfoVO();
        BeanUtils.copyProperties(templetInfoDTO, templetInfoVO);
        templetInfoVO.setShopId(shopId);
        List<TempletModuleDTO> templetModuleDTOList = templetModuleService.getBySelectTemplatModuleInfoNoPage(templetModuleBO);//所有的功能模块
        List<TempletModuleVO> templetModuleVOList = new ArrayList<>();
        for (TempletModuleDTO templetModuleDTO : templetModuleDTOList) {
            TempletModuleVO templetModuleVO = new TempletModuleVO();
            BeanUtils.copyProperties(templetModuleDTO, templetModuleVO);
            Long temModuleId = templetModuleVO.getId();//模板功能Id
            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_BANNER.getMsg())) {//banner
                EpgShopBannerBO epgShopBannerBO = new EpgShopBannerBO();
                epgShopBannerBO.setTempletId(templateId);
                epgShopBannerBO.setModuleId(temModuleId);
                epgShopBannerBO.setShopId(shopId);
                List<EpgShopBannerDTO> epgShopBannerDTOList = epgShopBannerService.getBySelectShopBannerInfoNoPage(epgShopBannerBO);
                //templetModuleVO.setEpgShopBannerDTOList(epgShopBannerDTOList);//店铺Banner添加进功能
                templetModuleVO.getMap().put(templetModuleVO.getKey(), epgShopBannerDTOList);
                templetModuleVOList.add(templetModuleVO);//添加功能集合
                continue;
            }
            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_MAIN_NAVIGATION.getMsg())) {//主导航
                EpgShopMainNavigationBO epgShopMainNavigationBO = new EpgShopMainNavigationBO();
                epgShopMainNavigationBO.setTempletId(templateId);
                epgShopMainNavigationBO.setModuleId(temModuleId);
                epgShopMainNavigationBO.setShopId(shopId);
                List<EpgShopMainNavigationDTO> epgShopMainNavigationDTOList = epgShopMainNavigationService.getBySelectShopMainNavigationInfoNoPage(epgShopMainNavigationBO);
                //templetModuleVO.setEpgShopMainNavigationDTOList(epgShopMainNavigationDTOList);//店铺主导航添加进功能
                templetModuleVO.getMap().put(templetModuleVO.getKey(), epgShopMainNavigationDTOList);
                templetModuleVOList.add(templetModuleVO);//添加功能集合
                continue;
            }

            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_SECTION.getMsg())) {//频道
                EpgShopSectionBO epgShopSectionBO = new EpgShopSectionBO();
                epgShopSectionBO.setShopId(shopId);
                epgShopSectionBO.setTempletId(templateId);
                epgShopSectionBO.setModuleId(temModuleId);
                List<EpgShopSectionDTO> epgShopSectionDTOList = epgShopSectionService.getBySelectShopSectionInfoNoPage(epgShopSectionBO);//所有频道
                List<EpgShopSectionVO> epgShopSectionVOList = new ArrayList<>();
                for (EpgShopSectionDTO epgShopSectionDTO : epgShopSectionDTOList) {//频道下商品添加
                    EpgShopSectionCommodityRelationBO epgShopSectionCommodityRelationBO = new EpgShopSectionCommodityRelationBO();
                    epgShopSectionCommodityRelationBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgShopSectionCommodityRelationBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
                    epgShopSectionCommodityRelationBO.setSectionId(epgShopSectionDTO.getId());
                    List<EpgShopSectionCommodityRelationVO> epgShopSectionCommodityRelationVOList = epgShopSectionCommodityRelationService.getBySelectShopSectionCommodityRelationInfo(epgShopSectionCommodityRelationBO);
                    EpgShopSectionVO epgShopSectionVO = new EpgShopSectionVO();
                    BeanUtils.copyProperties(epgShopSectionDTO, epgShopSectionVO);
                    epgShopSectionVO.setEpgShopSectionCommodityRelationVOList(epgShopSectionCommodityRelationVOList);
                    epgShopSectionVOList.add(epgShopSectionVO);//添加店铺频道栏目
                }
                //templetModuleVO.setEpgShopSectionVOList(epgShopSectionVOList);
                templetModuleVO.getMap().put(templetModuleVO.getKey(), epgShopSectionVOList);
                templetModuleVOList.add(templetModuleVO);//添加功能集合
                continue;
            }

            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_SHOP_INFO.getMsg())) {//店铺信息
                ShopInfoDTO shopInfoDTO = shopInfoService.selectById(shopId);
                templetModuleVO.getMap().put(templetModuleVO.getKey(), shopInfoDTO);
                templetModuleVOList.add(templetModuleVO);//添加功能集合
            }
        }
        templetInfoVO.setTempletModuleVOList(templetModuleVOList);//添加所有的已经启用模块信息并且排好序
        return templetInfoVO;
    }

    /**
     * 店铺的活动页模板
     *
     * @param templateId
     * @param shopId
     * @return
     */
    private TempletInfoVO getShopActiveTemInfo(Long templateId, Long shopId) {
        TempletModuleBO templetModuleBO = new TempletModuleBO();
        templetModuleBO.setTempletId(templateId);
        List<TempletModuleDTO> templetModuleDTOList = templetModuleService.getBySelectTemplatModuleInfoNoPage(templetModuleBO);//所有的功能模块
        TempletInfoDTO templetInfoDTO = selectById(templateId);//当前模板信息
        if (null == templetInfoDTO) {
            return null;
        }
        TempletInfoVO templetInfoVO = new TempletInfoVO();
        BeanUtils.copyProperties(templetInfoDTO, templetInfoVO);

        List<TempletModuleVO> templetModuleVOList = new ArrayList<>();
        for (TempletModuleDTO templetModuleDTO : templetModuleDTOList) {
            TempletModuleVO templetModuleVO = new TempletModuleVO();
            BeanUtils.copyProperties(templetModuleDTO, templetModuleVO);
            Long temModuleId = templetModuleVO.getId();//模板功能Id

            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_BANNER.getMsg())) {//banner
                EpgAggregationBannerBO epgAggregationBannerBO = new EpgAggregationBannerBO();
                epgAggregationBannerBO.setTempletId(templateId);
                epgAggregationBannerBO.setModuleId(temModuleId);
                List<EpgAggregationBannerDTO> epgAggregationBannerDTOList = epgAggregationBannerService.getBySelectBannerInfoNoPage(epgAggregationBannerBO);
                //templetModuleVO.setEpgBannerDTOList(epgBannerDTOList);//Banner添加进功能
                templetModuleVO.getMap().put(templetModuleVO.getKey(), epgAggregationBannerDTOList);
                templetModuleVOList.add(templetModuleVO);//添加功能集合
                continue;
            }

            if (templetModuleVO.getKey().equals(ShoppingCodeEnum.DECORATE_CATEGORY_INFO.getMsg())) {//分类
                EpgAggregationCategoryBO epgAggregationCategoryBO = new EpgAggregationCategoryBO();
                epgAggregationCategoryBO.setTempletId(templateId);
                epgAggregationCategoryBO.setModuleId(temModuleId);
                List<EpgAggregationCategoryDTO> epgAggregationCategoryDTOList = epgAggregationCategoryService.getBySelectCategoryInfoNoPage(epgAggregationCategoryBO);//所有分类
                List<EpgAggregationCategoryVO> epgAggregationCategoryVOList = new ArrayList<>();
                for (EpgAggregationCategoryDTO epgAggregationCategoryDTO : epgAggregationCategoryDTOList) {//分类商品添加
                    EpgAggregationCategoryCommodityRelationBO epgAggregationCategoryCommodityRelationBO = new EpgAggregationCategoryCommodityRelationBO();
                    epgAggregationCategoryCommodityRelationBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
                    epgAggregationCategoryCommodityRelationBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
                    epgAggregationCategoryCommodityRelationBO.setCategoryId(epgAggregationCategoryDTO.getId());
                    List<EpgAggregationCategoryCommodityRelationVO> epgAggregationCategoryCommodityRelationVOList = epgAggregationCategoryCommodityRelationService.getBySelectSectionCommodityRelationInfo(epgAggregationCategoryCommodityRelationBO);
                    EpgAggregationCategoryVO epgAggregationCategoryVO = new EpgAggregationCategoryVO();
                    BeanUtils.copyProperties(epgAggregationCategoryDTO, epgAggregationCategoryVO);
                    epgAggregationCategoryVO.setEpgAggregationCategoryCommodityRelationVOList(epgAggregationCategoryCommodityRelationVOList);
                    epgAggregationCategoryVOList.add(epgAggregationCategoryVO);//添加频道栏目
                }
                templetModuleVO.getMap().put(templetModuleVO.getKey(), epgAggregationCategoryVOList);
                templetModuleVOList.add(templetModuleVO);//添加功能集合
            }
        }
        templetInfoVO.setTempletModuleVOList(templetModuleVOList);//添加所有的已经启用模块信息并且排好序
        return templetInfoVO;
    }

    private void updateEsShop(Long shopId) {
        List<EsShopDTO> esShopDTOS = shopInfoService.queryEsShop(shopId.toString());
        shopSearchService.updateShop(shopId.toString(), "index_shop_2020", "typeName", 0, esShopDTOS.size(), esShopDTOS);
    }

}
